package com.ykdl.member.kid.fragments;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.R;
import com.ykdl.member.kid.adapters.MyCouponAdapter;
import com.ykdl.member.kid.adapters.MyCouponOnLineAdapter;
import com.ykdl.member.kid.beans.MyCouponOverViewBean;
import com.ykdl.member.kid.beans.OnLineCouponBean;
import com.ykdl.member.kid.beans.TicketBean;
import com.ykdl.member.kid.beans.TicketListBean;
import com.ykdl.member.kid.fragments.MyCouponHasUsedFragment.getDataTag;
import com.ykdl.member.kid.util.DisposalListUtils;
import com.ykdl.member.kid.util.DisposalListUtils.ItemBean;
import com.ykdl.member.kid.widget.RefreshListView;
import com.ykdl.member.kid.widget.RefreshListView.OnRefreshListener;
import com.ykdl.member.kid.xlist.XListView;
import com.ykdl.member.kid.xlist.XListView.IXListViewListener;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

/********************************************************************************
 * 
 ** 作者： jiafeiyu 创始时间：2015-4-27 描述： 我的优惠券之已过期--添加在线支付可用
 *********************************************************************************/
public class MyCouponOutOfDateFragment extends BaseFragment implements
		IXListViewListener {

	private XListView mRefListView;
	private MyCouponOnLineAdapter adapter;
	private List<TicketBean> tickets;
	private int cursor = 0;
	private int count = 20;
	private View convertView = null;
	private boolean isGoneBt = false;
	private List<TicketBean> mOffLineList = new ArrayList<TicketBean>();
	private List<OnLineCouponBean> mOnLineList = new ArrayList<OnLineCouponBean>();
	
	@Override
	protected void onNewCreateView(View view) {
		convertView = view;
		setCenterView(R.layout.fragment_coupon);
		initView();
	}

	private void initView() {
		
		tickets = new ArrayList<TicketBean>();
		adapter = new MyCouponOnLineAdapter(mContext, 3);
		mRefListView = (XListView) convertView.findViewById(R.id.mycoupon_list);
		mRefListView.setPullLoadEnable(true);
		// 允许下拉
		mRefListView.setPullRefreshEnable(true);
		// 设置监听器
		mRefListView.setXListViewListener(this);
		mRefListView.gonefoot(true);
		mRefListView.gonefoot();
		mRefListView.setAdapter(adapter);
		mRefListView.stopLoadMore();
		mRefListView.stopRefresh();
		mRefListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
			}
		});
		setIsShowLoad(true);
		getData();
	}


	// 获取网络数据
	public void getData() {
		
		String url = KidConfig.MY_COUPON_LIST_EXPIRED_OVERVIEW;
		Map<String, String> map = new HashMap<String, String>();
		map.put("cursor", String.valueOf(cursor));
		map.put("count", String.valueOf(count));
		HttpBaseRequest request = Wxxr.getInstance().getRequest(url, map, null);
		TaskManager.startHttpRequest(request, new getDataTag(),
				MyCouponOverViewBean.class);
	}

	class getDataTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			setIsShowLoad(false);
			// TODO Auto-generated method stub
			if (t instanceof MyCouponOverViewBean) {
				MyCouponOverViewBean bean = (MyCouponOverViewBean) t;
				if (bean.getError() == null) {
//					if (bean.getTickets() == null
//							|| bean.getTickets().size() == 0) {
//						mRefListView.setVisibility(View.GONE);
//						showErrorView(true, "暂无优惠券使用记录");
//					} else {
//						
					if (bean.getOffline_tickets()!=null) {
						
						mOffLineList.addAll(bean.getOffline_tickets());
					}
					if (bean.getOnline_tickets()!=null) {
						
						mOnLineList.addAll(bean.getOnline_tickets());
					}
					List<ItemBean> mData = DisposalListUtils.getInstance().disposalStoreAndCouponList(mOnLineList, mOffLineList);

					adapter.setmData(mData);
					adapter.setmOffLineList(mOffLineList);
					adapter.setmOnLineList(mOnLineList);
					
					adapter.notifyDataSetChanged();
					mRefListView.stopRefresh();
					mRefListView.stopLoadMore();
					
					mRefListView.stopLoadMore();
					mRefListView.gonefoot(false);
					isGoneBt = true;
					setIsShowLoad(false);
					//}
				}else {
					showErrorView(true, "暂无优惠券使用记录");
				}
			}

		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub

			showErrorView(true, null);
			setIsShowLoad(false);
			mRefListView.stopRefresh();
			mRefListView.stopLoadMore();
			return false;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
		}

	}

	@Override
	protected void tryAgain() {
		getData();
	}

	@Override
	public void onRefresh() {
		tickets.clear();
		mOffLineList.clear();
		mOnLineList.clear();
		cursor=0;
		getData();
	}

	@Override
	public void onLoadMore() {
		if (isGoneBt == false) {
			getData();
		} else {
			mRefListView.stopLoadMore();
			mRefListView.stopRefresh();
		}
	}
}
